package tree.tree2.symmetry.maxAndMinTree;

import tree.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @ClassName MaxTree
 * @Description TODO
 * @Author lenovo
 * @Date 2023-04-27 10:40
 * @Version 1.0
 * @Comment Magic. Do not touch.
 * If this comment is removed. the program will blow up
 */
public class MaxTree {
    /**
     * 给定一个二叉树，找出其最大深度。
     * <p>
     * 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
     * <p>
     * 说明: 叶子节点是指没有子节点的节点。
     * <p>
     * 示例：
     *
     * @param root
     * @return
     */
    public int maxDepth(TreeNode root) {
        int res = 0;
        if (root == null) return res;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            while (size > 0) {
                TreeNode poll = queue.poll();
                if (poll.left != null) {
                    queue.add(poll.left);
                }
                if (poll.right != null) {
                    queue.add(poll.right);
                }
                size--;
            }
            res++;
        }
        return res;
    }

    /**
     *     1
     *  2
     * @param root
     * @return
     */
    public int maxDepth1(TreeNode root) {
        if(root == null) return 0;

        int left = maxDepth1(root.left);
        int right = maxDepth1(root.right);
        return Math.max(left,right)+1;
    }

}